﻿/*
*AccessConfigurature .cs
*
* 功 能： 本地access数据库导入到服务器数据库,连接本地数据库和从本地库获得数据
* 类 名： AccessConfigurature
*
* Ver    变更日期  20130820   负责人  黄永生  变更内容
* ───────────────────────────────────

*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data.OleDb;
using System.Data;
using System.Web.UI.WebControls;
using DQYT.HTDZ.Common;
using System.Collections;
using System.Xml;

namespace DQYT.HTDZ.DBUtility
{
    public class AccessConfigurature //: IConfigurature
    {
        private string conStr = null;//连接本地库连接字符串
        private OleDbConnection connection = null;
        private DataTable dataTable = new DataTable();//保存获得的本地数据库数据
        private string[] restrictStr = new string[4];
        private string[] info = null;
        private string srcTableName = null;
        private GridView gridView;
        private string filePath;//= "System.Windows.Forms.Application.StartupPath"+"\\oracle_conf.xml";
        private List<ArrayList> listArrayList;
        private string[] congInfo;
        /// <summary>
        /// 构造连接字符串
        /// </summary>
        /// <param name="confInfo"></param>
        /// <param name="FilePath"></param>
        public AccessConfigurature(string[] confInfo, string FilePath)
        {
            conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + confInfo[0] + ";Persist Security Info=False";
            connection = new OleDbConnection(conStr);
            this.filePath = FilePath;
            info = confInfo;
        }

        public AccessConfigurature(string[] congInfo)
        {
            // TODO: Complete member initialization
            this.congInfo = congInfo;
        }
        #region IConfigurature Members

        /// <summary>
        /// 获得所某数据表所有数据列
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public List<string> GetColumnsScama(string tableName)
        {
            StringBuilder sb = new StringBuilder(tableName);
            sb.Remove(0, 1);
            sb.Remove(sb.Length - 1, 1);
            tableName = sb.ToString();
            srcTableName = tableName;
            dataTable.Columns.Clear();
            restrictStr[2] = tableName;
            List<string> comlis = new List<string>();
            connection.Open();
            try
            {
                dataTable = connection.GetSchema("Columns", restrictStr);
                DataRowCollection rows = dataTable.Rows;
                foreach (DataRow row in rows)
                {
                    comlis.Add(row[3].ToString());
                }
            }

            finally
            {
                connection.Close();
            }
            return comlis;
        }
        /// <summary>
        /// 获得所有数据表名称
        /// </summary>
        /// <returns></returns>
        public List<string> GetTableScama()
        {
            dataTable.Columns.Clear();
            List<string> lis = new List<string>();
            restrictStr[1] = null;
            connection.Open();
            try
            {
                dataTable = connection.GetSchema("tables", restrictStr);
                DataRowCollection rows = dataTable.Rows;
                foreach (DataRow row in rows)
                {
                    //lis.Add(row[1].ToString());
                    lis.Add("[" + row[2].ToString() + "]");

                }
                string s;
                for (int i = 0; i < lis.Count; i++)
                    s = lis[i].ToString();


            }
            finally
            {
                connection.Close();
            }
            return lis;
        }
        /// <summary>
        /// 获得查询结果集,返回一个DataTable
        /// </summary>
        /// <param name="queryStr"></param>
        /// <returns></returns>
        public DataTable GetQueryResultSet(string queryStr)
        {
            DataSet ds = new DataSet();
            OleDbCommand command = new OleDbCommand(queryStr, connection);
            OleDbDataAdapter adapter = new OleDbDataAdapter(command);
            try
            {
                adapter.Fill(ds);
            }
            catch
            {
                JsAlert.Alert("数据库连接错误");
            }
            return ds.Tables[0];
        }
        /// <summary>
        ///获得查询结果集,返回一个DataSet
        /// </summary>
        /// <param name="queryStr"></param>
        /// <returns></returns>
        public DataSet GetQueryResultDataSet(string queryStr)
        {
            DataSet ds = new DataSet();
            OleDbCommand command = new OleDbCommand(queryStr, connection);
            OleDbDataAdapter adapter = new OleDbDataAdapter(command);
            try
            {
                adapter.Fill(ds);
            }
            catch
            {
                JsAlert.Alert("数据库连接错误");
            }
            return ds;
        }
        /// <summary>
        /// 保存配置信息
        /// </summary>
        /// <param name="laList">字段对应数据</param>
        public void SaveConf(List<ArrayList> laList)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public void SaveConf(GridView dgv)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void LoadConf(DropDownList tableSelectBox, GridView dgv)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

    }
}
    